export const getSystemInfo = () => {
	const info = uni.getStorageSync('systemInfo')
	if (info) {
		for (let key in info) {
			systemInfo[key] = info[key]
		}
	} else {
		const platformName = checkPlatform()
		systemInfo.platformName = platformName
		let {
			statusBarHeight,
			safeAreaInsets,
			windowWidth,
			windowHeight
		} = uni.getSystemInfoSync();
		systemInfo.statusHeight = statusBarHeight
		systemInfo.safeAreaHeight = safeAreaInsets.bottom
		systemInfo.windowHeight = windowHeight

		if (platformName === 'MP-WEIXIN') {
			let {
				top,
				height,
				left,
				right
			} = uni.getMenuButtonBoundingClientRect()
			systemInfo.menuHeight = (top - statusBarHeight) * 2 + height // 上下都有对应空位
			systemInfo.menuWidth = windowWidth - left + (windowWidth - right) // 左右都有对应空位
		}
		// 支付宝小程序 多一个收藏按钮
		if (platformName === 'MP-ALIPAY') {
			let {
				top,
				height,
				right,
				optionMenuLeft
			} = uni.getMenuButtonBoundingClientRect()
			systemInfo.menuHeight = (top - statusBarHeight) * 2 + height // 上下都有对应空位
			systemInfo.menuWidth = (windowWidth - right) * 2 + (right - optionMenuLeft) // 左右都有对应空位
		}
		uni.setStorageSync('systemInfo', systemInfo)
	}
	return systemInfo
}
/**
 * 数组去重
 * @param array 数组
 */
export const uniqueArray = (array) => {
	return Array.from(new Set(array));
}
/**
 * 对象数组去重
 * @param array 对象数组
 * @param key 去重的字段名
 */
export const uniqueArrayWithObjects = (array, key) => {
	const unique = [];
	const map = new Map();

	array.forEach(item => {
		if (!map.has(item[key])) {
			map.set(item[key], true);
			unique.push(item);
		}
	});

	return unique;
}

/**
 * 是否为空
 * @param value
 */
export const isNullOrEmpty = (value) => {
	return value === null ||
		value === undefined ||
		value === "" ||
		value === "null" ||
		value === "NaN" ||
		value === "undefined"
}
/**
 * 格式化时间
 */
export const formatDate = (date, format) => {
	const pad = (n) => (n < 10 ? '0' + n : n);

	return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(match) {
		switch (match) {
			case 'yyyy':
				return date.getFullYear();
			case 'MM':
				return pad(date.getMonth() + 1);
			case 'dd':
				return pad(date.getDate());
			case 'HH':
				return pad(date.getHours());
			case 'mm':
				return pad(date.getMinutes());
			case 'ss':
				return pad(date.getSeconds());
			default:
				return match;
		}
	});
}
/**
 * 格式化字符串时间
 */
export const parseDate = (dateString) => {
	// 尝试解析 yyyy-MM-dd 格式
	const regex = /^(\d{4})-(\d{2})-(\d{2})$/;
	const match = dateString.match(regex);

	if (match) {
		const year = parseInt(match[1], 10);
		const month = parseInt(match[2], 10) - 1; // 月份从0开始
		const day = parseInt(match[3], 10);
		return new Date(year, month, day);
	}

	// 尝试解析其他格式
	const date = new Date(dateString);
	if (!isNaN(date.getTime())) {
		return date;
	}

	// 无法解析时返回 null 或抛出错误
	throw new Error('Invalid date string');
};
/**
 * 比较两个时间大小
 * @@return {Number} -1 date1 在 date2 之前,1 date1 在 date2 之后,0 date1 和 date2 相同
 */
export const compareDates = (date1, date2) => {
	const time1 = date1.getTime();
	const time2 = date2.getTime();

	if (time1 < time2) {
		return -1; // date1 在 date2 之前
	} else if (time1 > time2) {
		return 1; // date1 在 date2 之后
	} else {
		return 0; // date1 和 date2 相同
	}
};
/**
 * 数组所有对象的指定字段全部为value
 */
export const isAllFieldEqual = (arr, fieldName, value) => {
	return arr.every(obj => obj[fieldName] === value);
};

export class MyURLSearchParams {
	constructor(input) {
		this.params = new Map();

		if (typeof input === 'string') {
			input.split('&').forEach(param => {
				const [key, value] = param.split('=');
				this.append(decodeURIComponent(key), decodeURIComponent(value));
			});
		} else if (typeof input === 'object') {
			for (let [key, value] of Object.entries(input)) {
				if (Array.isArray(value)) {
					value.forEach(val => this.append(key, val));
				} else {
					this.append(key, value);
				}
			}
		}
	}

	append(name, value) {
		if (this.params.has(name)) {
			this.params.get(name).push(value);
		} else {
			this.params.set(name, [value]);
		}
	}

	delete(name) {
		this.params.delete(name);
	}

	get(name) {
		return this.params.get(name)?.[0] || null;
	}

	getAll(name) {
		return this.params.get(name) || [];
	}

	has(name) {
		return this.params.has(name);
	}

	set(name, value) {
		this.params.set(name, [value]);
	}

	toString() {
		let result = [];
		for (let [key, values] of this.params) {
			values.forEach(value => {
				result.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
			});
		}
		return result.join('&');
	}
}
/**
 * 正则手机号验证
 */
export const regexPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/

/**
 * 时间分组
 */
// export const groupByTime = (list, timeFieldName) => {
// 	const arr = {}
// 	list.forEach(e => {
// 		arr[e[timeFieldName]] = arr[e[timeFieldName]] && arr[e[timeFieldName]] ||
// 	})
// }

export const genOrderNumber = (tag) => {
	const timestamp = new Date().getTime();
	const randomNum = Math.floor(Math.random() * 100000);
	return `YXY-${tag}-${timestamp}${randomNum}`;
}